/***
	Copyright (c) 2010 CommonsWare, LLC
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may
	not use this file except in compliance with the License. You may obtain
	a copy of the License at
		http://www.apache.org/licenses/LICENSE-2.0
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
 */

package com.viethp.mt.activities;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver {
	private HttpResponse response;

	public C2DMReceiver() {
		super("aki@hirevietnamese.com");
	}

	@Override
	public void onRegistered(Context context, String registrationId) {
		HttpClient httpclient = new DefaultHttpClient();
		Log.w("C2DMReceiver-onRegistered", "Sending request to Server");

		HttpGet httpget = new HttpGet(getResources().getString(
				R.string.server_url)
				+ "?act=2&deviceToken="
				+ registrationId
				+ "&deviceId="
				+ Settings.Secure.ANDROID_ID);
		try {
			setResponse(httpclient.execute(httpget));
		} catch (Exception e) {
			Log.e("C2DMReceiver-onRegistered", e.getMessage());
		}
		Log.w("C2DMReceiver-onRegistered", registrationId);
	}

	@Override
	public void onUnregistered(Context context) {
		Log.w("C2DMReceiver-onUnregistered", "got here!");
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.w("C2DMReceiver-onError", errorId);
	}

	@Override
	protected void onMessage(Context contxt, Intent intent) {
		Log.w("C2DMReceiver", intent.getStringExtra("message"));
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		int icon = R.drawable.ic_launcher; // icon from resources
		CharSequence tickerText = "You got message !!!"; // ticker-text
		long when = System.currentTimeMillis(); // notification time
		Context context = getApplicationContext(); // application Context
		CharSequence contentTitle = "mTrade notification"; // expanded message
		// title
		CharSequence contentText = intent.getStringExtra("message"); // expanded
		// message
		// text

		Intent notificationIntent = new Intent(this, AMain.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		// the next two lines initialize the Notification, using the
		// configurations above
		Notification notification = new Notification(icon, tickerText, when);
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.ledARGB = 0xff00ff00;
		notification.ledOnMS = 300;
		notification.ledOffMS = 1000;
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		notification.sound = Uri
				.parse("android.resource://com.push.activities/raw/iphone_notification");
		// notification.defaults |= Notification.DEFAULT_VIBRATE;
		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);
		notificationManager.notify((int) when, notification);

		Log.w("C2DMReceiver", "finish");
	}

	public HttpResponse getResponse() {
		return response;
	}

	public void setResponse(HttpResponse response) {
		this.response = response;
	}
}
